// mircoApp/new_coupon/new_coupon/new_coupon.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "0",
    status: 1,
    child_status: "",
    coupon_type: [],
    exchange: 0, //兑换入口
    flash_sale: 0, //秒杀入口
    coupon_list: [],

    isLoadMore: true,
    page: 1,
    total_page: 0,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  to_exchange() {
    wx.navigateTo({
      url: '/mircoApp/new_coupon/exchange/exchange',
    })
  },
  onChange(e) {
    this.setData({
      active: e.detail.index,
      coupon_list: []
    })
    this.getData();
  },
  to_carNumber(e) {
    let {
      id
    } = e.target.dataset;
    wx.navigateTo({
      url: '/mircoApp/new_coupon/carNumber/carNumber?id=' + id,
    })
  },
  toDetail(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/mircoApp/new_coupon/new_coupon_detail/new_coupon_detail?id=' + id,
    })
  },
  tapNavChild(e) {
    let {
      id
    } = e.target.dataset;
    console.log(id)
    this.setData({
      child_status: id,
      coupon_list: []
    })
    this.getList();
  },
  tapNav(e) {
    let {
      id
    } = e.target.dataset;
    this.setData({
      status: id,
      child_status: '',
      coupon_list: []
    })
    for (let i = 0; i < this.data.coupon_type.length; i++) {
      if (this.data.coupon_type[i].id == this.data.status) {
        if (this.data.coupon_type[i].child && this.data.coupon_type[i].child.length > 0) {
          this.data.child_status = this.data.coupon_type[i].child[0].id;
          break;
        }
      }
    }
    this.setData({
      child_status: this.data.child_status
    })
    this.getList();
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.member-coupon.index")
    })
    console.log(data, 'ww')
    if (data.result) {
      this.setData({
        coupon_type: data.data.coupon_type,
        status: data.data.coupon_type[0].id,
        flash_sale: data.data.flash_sale,
        exchange: data.data.exchange,
      })
      for (let i = 0; i < this.data.exchange.length; i++) {
        if (this.data.exchange[i].child && this.data.exchange[i].child.length > 0) {
          this.setData({
            child_status: this.data.exchange[i].child[0].id
          })
          break;
        }
      }
      this.getList()
    } else {
      app.tips(data.msg);
    }
  },
  async getList() {
    let json = {
      status: this.data.active,
      parent_coupon_type: this.data.status,
      coupon_type: this.data.child_status
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.member-coupon.get-list"),
      data: json
    })
    if (data.result) {
      this.setData({
        coupon_list: data.data.list.data,
        total_page: data.data.list.last_page,
        isLoadMore: true,
        page: 1,
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.member-coupon.get-list"),
        data: {
          page: this.data.page,
          status: this.data.active,
          parent_coupon_type: this.data.status,
          coupon_type: this.data.child_status
        }
      })
      if (data.result) {
        this.setData({
          coupon_list: this.data.coupon_list.concat(data.data.list.data)
        })
      } else {
        this.setData({
          page: this.data.page - 1,
          isLoadMore: false
        })
        app.tips(data.msg)
      }
    }
  }
})